# \per\mnxc\survival_evolution\po\game.py
# -*- coding: utf-8 -*-
import pygame
import pygame_gui

from per.mnxc.survival_evolution.constants.constants import SCREEN_WIDTH, SCREEN_HEIGHT
from per.mnxc.survival_evolution.po.world import World


class Game:
    def __init__(self,name:str= "生物进化模拟器", width: int = SCREEN_WIDTH, height: int = SCREEN_HEIGHT, is_running: bool = True):
        pygame.init()
        pygame.display.set_caption(name)
        self.name = name
        self.is_running = is_running
        self.width = width
        self.height = height
        self.nav_height = 50
        self.left_aside_width = 200
        self.right_aside_width = 200
        self.screen: pygame.Surface = pygame.display.set_mode((self.width, self.height))
        self.ui_manager = pygame_gui.UIManager((self.width, self.height),starting_language='zh')
        self.clock = pygame.time.Clock()
        # 创建导航栏、左侧边栏和右侧边栏
        self.nav_panel = pygame_gui.elements.UIPanel(
            relative_rect=pygame.Rect((0, 0), (self.width, self.nav_height)),
            manager=self.ui_manager
        )
        self.left_aside_panel = pygame_gui.elements.UIPanel(
            relative_rect=pygame.Rect((0, self.nav_height), (self.left_aside_width, self.height - self.nav_height)),
            manager=self.ui_manager
        )
        self.right_aside_panel = pygame_gui.elements.UIPanel(
            relative_rect=pygame.Rect((self.width - self.right_aside_width, self.nav_height),
                                      (self.right_aside_width, self.height - self.nav_height)),
            manager=self.ui_manager
        )
        # 主面板
        self.main_panel = pygame_gui.elements.UIPanel(
            relative_rect=pygame.Rect((self.left_aside_width, self.nav_height),
                                      (self.width - self.left_aside_width - self.right_aside_width,
                                       self.height - self.nav_height)),
            manager=self.ui_manager
        )
        # 创建世界
        self.screen.subsurface(pygame.Rect(center, (self.width - self.left_aside_width - self.right_aside_width,)))
        center = self.main_panel.get_abs_rect().center
        self.world = World()
        # 运行游戏
        self.run()

    def run(self):
        while self.is_running:
            delta_time = self.clock.tick(60)/1000.0
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.is_running = False
                    pygame.quit()
                    return
                self.ui_manager.process_events(event)
            self.ui_manager.update(delta_time)
            self.ui_manager.draw_ui(self.screen)
            pygame.display.update()


